package imper.graphic.view;

import java.util.ArrayList;
import java.util.List;

import imper.engine.entity.CivilUnitType;
import imper.engine.util.ParameterManager;
import imper.graphic.ImageManager;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import TWLSlick.RootPane;
import de.matthiasmann.twl.Button;

public class CivilUnitView extends BarMenuView
{

	private List<CivilUnitGroup> _groupList = null;

	@Override
	public void init(GameContainer pGameContainer, StateBasedGame pStateBasedGame) throws SlickException
	{
		super.init(pGameContainer, pStateBasedGame);
		//
		_groupList = new ArrayList<CivilUnitGroup>();
		//
		int nb = 0;
		for (CivilUnitType civilUnit : ParameterManager.getInstance().getListCivilUnitType().values())
		{
			int posX = 40 + ((nb % 3) * 250);
			int posY = 50 + ((nb / 3) * 120);
			_groupList.add(new CivilUnitGroup(civilUnit, this, posX, posY));
			nb++;
		}
	}

	@Override
	public void render(GameContainer pGameContainer, StateBasedGame pStateBasedGame, Graphics pGraphics) throws SlickException
	{
		// TODO Auto-generated method stub
		pGraphics.drawString("Civil", 100, 100);
		//
		for (CivilUnitGroup civilGroup : _groupList)
		{
			civilGroup.render(pGameContainer, pStateBasedGame, pGraphics);
		}
	}

	@Override
	protected void setViewRootPane(RootPane pRootPane)
	{
		for (CivilUnitGroup civilGroup : _groupList)
		{
			civilGroup.setViewRootPane(pRootPane);
		}
	}

	@Override
	protected void setViewLayout()
	{
		for (CivilUnitGroup civilGroup : _groupList)
		{
			civilGroup.setViewLayout();
		}
	}
	
	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getID()
	{
		return EnumView.CIVILUNITVIEW.getId();
	}

	public class CivilUnitGroup
	{
		private CivilUnitType _civilUnit = null;

		private CivilUnitView _view;

		private int _posX;
		private int _posY;

		private Button _buttonMore = null;

		private Button _buttonLess = null;

		public CivilUnitGroup(CivilUnitType pCivilUnit, CivilUnitView pView, int pPosX, int pPosY)
		{
			_posX = pPosX;
			_posY = pPosY;
			_civilUnit = pCivilUnit;
			_view = pView;
		}

		protected void setViewRootPane(RootPane pRootPane)
		{
			_buttonMore = new Button("+");
			//_buttonMore.addCallback(new AddIndustryListener(_industry, _view));
			pRootPane.add(_buttonMore);
			//
			_buttonLess = new Button("-");
			//_buttonLess.addCallback(new RemoveIndustryListener(_industry, _view));
			pRootPane.add(_buttonLess);
		}

		protected void setViewLayout()
		{
			//
			_buttonMore.adjustSize();
			_buttonMore.setSize(20, 20);
			_buttonMore.setPosition(_posX + 50, _posY );
			//
			_buttonLess.adjustSize();
			_buttonLess.setSize(20, 20);
			_buttonLess.setPosition(_posX + 50, _posY + 20);
			//

		}

		public void render(GameContainer pGameContainer, StateBasedGame pStateBasedGame, Graphics pGraphics) throws SlickException
		{
			Image image = ImageManager.getInstance().getImage(ImageManager.CIVIL_UNIT, _civilUnit.getId());
			pGraphics.drawImage(image, _posX, _posY );
		}

	}



}
